<?php
namespace Pay\Wxpay;

class GetOpenid
{
    protected $url = 'https://api.weixin.qq.com/sns/oauth2/access_token';

    public function getData($config, $code)
    {
        $url = $this->url . '?appid=' . $config['appId'] . '&grant_type=authorization_code&secret=' . $config['secret'] . '&code=' . $code;

        $data = $this->doPay($url);
        $data = json_decode($data, true);
        if ($data['openid']) {
            return $data['openid'];
        }
        return false;
    }

    protected function doPay($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_TIMEOUT, 30000);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_POST, false);
        $res = curl_exec($curl);
        curl_close($curl);
        return $res;
    }
}
?>
